package cn.jbolt.ec.commodity;

import cn.jbolt.base.JBoltBaseService;
import cn.jbolt.common.bean.JsTreeBean;
import cn.jbolt.common.config.Msg;
import cn.jbolt.common.model.BecRetailerCommodityType;
import cn.jbolt.common.model.SystemLog;
import cn.jbolt.common.util.ArrayUtil;
import com.jfinal.kit.Kv;
import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.DbTemplate;
import com.jfinal.plugin.activerecord.Record;

import java.util.ArrayList;
import java.util.List;

public class CommodityTypeService extends JBoltBaseService<BecRetailerCommodityType> {
    private BecRetailerCommodityType dao = new BecRetailerCommodityType().dao();
    @Override
    protected BecRetailerCommodityType dao() {
        return dao;
    }

    public List<Record> get_top_types(int retailer_id){
        Kv cond = Kv.by("retailer_id",retailer_id);
        return dbTemplate("ec.mall.shop.commodity.commodity_top_type",cond).find();


    }

    public List<Record> get_sub_types(int pid){
        Kv cond = Kv.by("pid",pid);
        return dbTemplate("ec.mall.shop.commodity.commodity_sub_type",cond).find();


    }



    /***************************************
     * 后台方法
     * @author  zddomi
     * ***********************************
     */


    /**
     * 得到后台分类数据树 包含所有数据
     * @param checkedId 默认选中节点
     * @return
     */
    public List<JsTreeBean> get_mgrTree(Integer checkedId) {
        List<BecRetailerCommodityType> list = get_mgrList();
        return convertJsTreeDatas(list, checkedId);
    }

    /**
     * 获取分类数据中的所有后端分类数据
     * @return
     */
    public List<BecRetailerCommodityType> get_mgrList() {
        return get_type_with_enabled(null);
    }

    /**
     * 获取分类树，并对数据进行处理
     * @param listBecRetailerCommodityType
     * @param selectedId
     * @return
     */
    private List<JsTreeBean> convertJsTreeDatas(List<BecRetailerCommodityType> listBecRetailerCommodityType,
                                                Integer selectedId) {

        List<JsTreeBean> treeBeans = new ArrayList<JsTreeBean>();
        treeBeans.add(new JsTreeBean(0, 0, "全部分类", true, "defaults"));
        if (listBecRetailerCommodityType.size() > 0) {
            String[] keys = null;
            if (isOk(selectedId)) {
                String key = null;
                BecRetailerCommodityType becRetailerCommodityType = findById(selectedId);
                key = becRetailerCommodityType.getPid() + "_" + becRetailerCommodityType.getId();
                keys = ArrayUtil.from(key, "_");
            }
            for (BecRetailerCommodityType becRetailerCommodityType : listBecRetailerCommodityType) {
                boolean opened = false;
                boolean selected = false;
                String type = "default";

                if (ArrayUtil.contains(keys, becRetailerCommodityType.getId())) {
                    selected = false;
                    opened = true;
                }
                if (selectedId != null && selectedId > 0 && becRetailerCommodityType.getId().intValue() == selectedId.intValue()) {
                    selected = true;
                    opened = true;
                }
                treeBeans.add(convertToJsTreeBean(becRetailerCommodityType, opened, selected, type));
            }
        }
        return treeBeans;
    }

    /**
     * 从封装方法JsTressBean中进行判断
     * @param becRetailerCommodityType
     * @param opened
     * @param selected
     * @param type
     * @return
     */
    private JsTreeBean convertToJsTreeBean(BecRetailerCommodityType becRetailerCommodityType, boolean opened, boolean selected, String type) {
        return new JsTreeBean(becRetailerCommodityType.getId(), becRetailerCommodityType.getPid(),
                becRetailerCommodityType.getName(), opened, selected, type);
    }

    /**
     * 获取分类数据
     * @param enabled 启用禁用状态
     * @return
     */

    public List<BecRetailerCommodityType> get_type_with_enabled(Boolean enabled) {
        Kv kv = Kv.create();
        if (enabled != null) {
            kv.set("enabled", enabled);

        }
        kv.set("is_del",0);
        return getCommonList(kv);
    }

//    public List<BecRetailerCommodityT
//    ype> get_type_with_enabled(Boolean enabled) {
//        Kv kv = Kv.create();
//        if (enabled != null) {
//            kv.set("enabled", enabled);
//        }
//        List<BecRetailerCommodityType> list=getCommonList(kv);
//        int i=1;
//        while (true){
//            if (i==list.size()-1) break;
//            if (get_top_by_pid(list.get(i).getPid())==null&&list.get(i).getPid()!=0){
//                list.remove(i);
//            }
//            i++;
//        }
//
//        return list;
//    }
//
//    public BecRetailerCommodityType get_top_by_pid(Integer pid){
//        System.out.println(1);
//        BecRetailerCommodityType becRetailerCommodityType=dao().findById(pid);
//        if (becRetailerCommodityType==null){
//            System.out.println(23232323);
//            return null;
//        }
//        else return becRetailerCommodityType;
//    }

    /**
     * 保存新增商品分类
     * @param userId
     * @param becRetailerCommodityType
     * @return
     */
    public Ret save_Commodity_Type(Integer userId, BecRetailerCommodityType becRetailerCommodityType) {
        if (becRetailerCommodityType == null || isOk(becRetailerCommodityType.getId()) || notOk(becRetailerCommodityType.getName())) {
            return fail(Msg.PARAM_ERROR);
        }



        if (notOk(becRetailerCommodityType.getEnabled())) {
            becRetailerCommodityType.setEnabled(false);
        }
        boolean success = becRetailerCommodityType.save();
        if (success) {
            addSaveSystemLog(becRetailerCommodityType.getId(), userId, SystemLog.TARGETTYPE_MALL_GOODS_BACK_CATEGORY, becRetailerCommodityType.getName());
            return success(becRetailerCommodityType.getId(), Msg.SUCCESS);
        }
        return fail(Msg.FAIL);
    }

    /**
     * 修改商品分类
     * @param userId
     * @param becRetailerCommodityType
     * @return
     */
    public Ret update_Commodity_Type(Integer userId, BecRetailerCommodityType becRetailerCommodityType) {
        System.out.println(becRetailerCommodityType + "aaaaaaaaaa");
        if (becRetailerCommodityType == null || notOk(becRetailerCommodityType.getId()) || notOk(becRetailerCommodityType.getName())) {
            return fail(Msg.PARAM_ERROR);
        }



        if (notOk(becRetailerCommodityType.getEnabled())) {
            becRetailerCommodityType.setEnabled(false);
        }
        BecRetailerCommodityType becRetailerCommodityType1 = findById(becRetailerCommodityType.getId());
        if (becRetailerCommodityType1 == null) {
            return fail(Msg.DATA_NOT_EXIST);
        }
        if (becRetailerCommodityType1.getEnabled() != null && becRetailerCommodityType1.getEnabled() == true && becRetailerCommodityType.getEnabled() == false) {
            //说明这次修改是可能禁用 需要检测是否正在被使用
            String msg = checkInUse(becRetailerCommodityType);
            if (StrKit.notBlank(msg)) {
                return fail(msg);
            }
        }
        boolean success = becRetailerCommodityType.update();
        if (success) {
            addUpdateSystemLog(becRetailerCommodityType1.getId(), userId, SystemLog.TARGETTYPE_MALL_GOODS_BACK_CATEGORY, becRetailerCommodityType1.getName());
            return success(becRetailerCommodityType.getId(), Msg.SUCCESS);

        }
        return FAIL;
    }

    /**
     * 软删除商品分类
     * @param userId
     * @param id
     * @return
     */
    public Ret delete_Commodity_Type(Integer userId, Integer id) {
        BecRetailerCommodityType becRetailerCommodityType = new BecRetailerCommodityType().findById(id);
        find_child_by_pid(id);
        becRetailerCommodityType.setIsDel(true);
        Ret ret=fail("删除失败");
        if (becRetailerCommodityType.update())
        {
             ret=success("删除成功");
        }

        return ret;
    }

    public void find_child_by_pid(Integer pid) {
        BecRetailerCommodityType becRetailerCommodityType = new BecRetailerCommodityType();
        List<BecRetailerCommodityType> list = dao().find("select id from bec_retailer_commodity_type where pid = ?", pid);

        if (list.size() != 0) {


            for (int i = 0; i < list.size(); i++) {
                BecRetailerCommodityType delete_id = dao().findById(list.get(i).getId());
                if (delete_id!=null){
                    delete_id.setIsDel(true);
                    delete_id.update();
                    find_child_by_pid(delete_id.getId());
                }



            }
        }
    }


    /**
     * 得到后台分类数据树 包含所有数据
     * @param checkedId 默认选中节点
     * @return
     */
    public List<JsTreeBean> getMgrTree(Integer checkedId) {
        List<BecRetailerCommodityType> list = getMgrList();
        return convertJsTreeDatas(list, checkedId);
    }

    /**
     * 获取分类数据中的所有后端分类数据
     *
     * @return
     */
    public List<BecRetailerCommodityType> getMgrList() {
        return get_type_with_enabled(null);
    }

    /**
     * 根据PID获取分类数据 后端启用的数据
     * @param pid
     * @return
     */
    public List<BecRetailerCommodityType> getEnableListByPid(Integer pid) {
        return get_type_by_pid(pid, true);

    }

    /**
     * 根据PID获取分类数据
     * @param pid
     * @param enabled 启用禁用状态
     * @return
     */
    public List<BecRetailerCommodityType> get_type_by_pid(Integer pid, Boolean enabled) {
        Kv kv = Kv.by("pid", pid);
        if (enabled != null) {
            kv.set("enabled", enabled);
            kv.set("is_del",0);
        }
        return getCommonList(kv);
    }


}
